// 简单的DeepSeek API测试脚本
require('dotenv').config();

async function testDeepSeekAPI() {
    console.log('🧪 开始测试DeepSeek API集成...\n');

    // 检查环境变量
    console.log('1. 检查环境变量配置:');
    console.log(`   DEEPSEEK_API_KEY: ${process.env.DEEPSEEK_API_KEY ? '✅ 已配置' : '❌ 未配置'}`);
    console.log(`   DEEPSEEK_BASE_URL: ${process.env.DEEPSEEK_BASE_URL || '使用默认值'}`);
    console.log(`   DEEPSEEK_MODEL: ${process.env.DEEPSEEK_MODEL || '使用默认值'}\n`);

    if (!process.env.DEEPSEEK_API_KEY) {
        console.log('❌ 请在.env文件中设置DEEPSEEK_API_KEY');
        return;
    }

    try {
        // 测试模块导入
        console.log('2. 测试模块导入:');
        const { DeepSeekClient } = require('./dist/modules/deepseek-client.js');
        console.log('   ✅ DeepSeek客户端模块导入成功\n');

        // 创建客户端实例
        console.log('3. 创建DeepSeek客户端:');
        const config = {
            apiKey: process.env.DEEPSEEK_API_KEY,
            baseURL: process.env.DEEPSEEK_BASE_URL || 'https://api.deepseek.com/v1',
            model: process.env.DEEPSEEK_MODEL || 'deepseek-chat',
            maxTokens: parseInt(process.env.DEEPSEEK_MAX_TOKENS || '2048')
        };

        const client = new DeepSeekClient(config);
        console.log('   ✅ DeepSeek客户端创建成功\n');

        // 测试简单任务分析
        console.log('4. 测试任务分析功能:');
        console.log('   任务: "显示当前系统时间和用户信息"');

        const response = await client.analyzeTask(
            "显示当前系统时间和用户信息",
            "测试环境: Ubuntu 20.04"
        );

        console.log('   ✅ DeepSeek响应成功');
        console.log('   📋 分析结果:');
        console.log(`   推理: ${response.reasoning?.substring(0, 100)}...`);
        console.log(`   生成命令数量: ${response.commands?.length || 0}`);

        if (response.commands && response.commands.length > 0) {
            console.log('   🛠️  建议命令:');
            response.commands.slice(0, 3).forEach((cmd, index) => {
                console.log(`     ${index + 1}. ${cmd}`);
            });
        }

        console.log('\n🎉 所有测试通过！DeepSeek API集成正常工作。');

    } catch (error) {
        console.log(`❌ 测试失败: ${error.message}`);
        console.log('\n🔍 故障排除建议:');
        console.log('1. 检查API密钥是否正确');
        console.log('2. 确认网络连接正常');
        console.log('3. 检查防火墙设置');
        console.log('4. 运行 npm run build 确保代码已编译');
    }
}

// 运行测试
testDeepSeekAPI().catch(console.error);